﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;


namespace MishraReader.Windows.Controls
{
    public class StepItem : ContentControl
    {
        public StepItem()
        {
            HorizontalContentAlignment = HorizontalAlignment.Stretch;
            VerticalContentAlignment = VerticalAlignment.Stretch;
        }
    }

    [TemplatePart(Name = "PART_SelectedContentHost", Type = typeof(ContentControl))]
    public class StepControl : Selector
    {
        public static readonly DependencyProperty SelectedContentProperty = DependencyProperty.Register("SelectedContent", typeof(Object), typeof(StepControl),
                                                                                                        new UIPropertyMetadata(null));

        public static readonly DependencyProperty TransitionStoryboardProperty = DependencyProperty.Register("TransitionStoryboard", typeof(Storyboard),
                                                                                                             typeof(StepControl), new UIPropertyMetadata(null));

        static StepControl()
        {
            IsTabStopProperty.OverrideMetadata(typeof(StepControl), new FrameworkPropertyMetadata(false));
            DefaultStyleKeyProperty.OverrideMetadata(typeof(StepControl), new FrameworkPropertyMetadata(typeof(StepControl)));
        }

        public StepControl()
        {
            HorizontalContentAlignment = HorizontalAlignment.Stretch;
            VerticalContentAlignment = VerticalAlignment.Stretch;
        }

        public Object SelectedContent
        {
            get { return GetValue(SelectedContentProperty); }
            protected set { SetValue(SelectedContentProperty, value); }
        }

        public Storyboard TransitionStoryboard
        {
            get { return (Storyboard)GetValue(TransitionStoryboardProperty); }
            set { SetValue(TransitionStoryboardProperty, value); }
        }

        internal ContentPresenter SelectedContentPresenter
        {
            get { return GetTemplateChild("PART_SelectedContentHost") as ContentPresenter; }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            UpdateSelectedContent();
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new StepItem();
        }


        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is StepItem;
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            ItemContainerGenerator.StatusChanged += OnGeneratorStatusChanged;
        }

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);

            var selectedTabItem = GetSelectedStepItem();


            UpdateSelectedContent();

            if (selectedTabItem != null)
            {
                var selectedContentPresenter = SelectedContentPresenter;

                if (selectedContentPresenter != null)
                {
                    var sb = TransitionStoryboard;
                    if (sb != null)
                        selectedContentPresenter.BeginStoryboard(TransitionStoryboard);
                }
            }
        }

        private StepItem GetSelectedStepItem()
        {
            var selectedItem = SelectedItem;
            if (selectedItem == null)
            {
                return null;
            }
            var container = selectedItem as StepItem;
            if (container == null)
            {
                container = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as StepItem;
                if ((container != null) && !Equals(selectedItem, ItemContainerGenerator.ItemFromContainer(container)))
                {
                    container = ItemContainerGenerator.ContainerFromItem(selectedItem) as StepItem;
                }
            }
            return container;
        }

        private void UpdateSelectedContent()
        {
            if (SelectedIndex < 0)
            {
                SelectedContent = null;
            }
            else
            {
                var selectedTabItem = GetSelectedStepItem();
                if (selectedTabItem != null)
                {
                    SelectedContent = selectedTabItem.Content;
                    var selectedContentPresenter = SelectedContentPresenter;
                    if (selectedContentPresenter != null)
                    {
                        selectedContentPresenter.HorizontalAlignment = selectedTabItem.HorizontalContentAlignment;
                        selectedContentPresenter.VerticalAlignment = selectedTabItem.VerticalContentAlignment;
                    }
                }
            }
        }

        private void OnGeneratorStatusChanged(object sender, EventArgs e)
        {
            if (ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
            {
                if (HasItems && (SelectedItem == null))
                {
                    SetCurrentValue(SelectedIndexProperty, 0);
                }
                UpdateSelectedContent();
            }
        }
    }
}